<template>
  <div>
    <MyTable :list="list">
      <template #header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="scope">
        <td>{{ scope.index + 1 }}</td>
        <td>{{ scope.row.goods_name }}</td>
        <td>{{ scope.row.goods_price }}</td>
        <td>
          <input type="text" 
          v-if="scope.row.inputVisible"
          class="tag-input form-control"
          v-fff
          v-model="scope.row.inputValue"
          style="width: 100px;"
          @keyup.enter="enterFn(scope.row)"
          @keyup.esc="scope.row.inputValue = ''"
          @blur="scope.row.inputVisible = false"
          >
          <button v-else 
          @click="scope.row.inputVisible = true"
          style="display: block;" 
          class="btn btn-primary btn-sm add-tag"
          >+tabs</button>
          <span v-for="(obj, ind) in scope.row.tags" :key="ind"
          class="badge bg-warning"
          >{{ obj }}</span>
        </td>
        <td><button 
          @click="delFn(scope.row.id)"
          class="btn btn-danger btn-sm"
          >删除</button></td>
      </template>
    </MyTable>
  </div>
</template>

<script>

import MyTable from "../components/MyTable";
import axios from "axios";
axios.defaults.baseURL = "https://www.escook.cn";
export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    };
  },
  created() {
    axios({
      url: '/api/goods',
    }).then(res => {
      this.list = res.data.data
    }).catch(err => { 
      console.log(err)
    })
  },
  methods: {
    delFn(id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    enterFn(obj) {
      if (obj.inputValue.trim() === '') {
        alert('请输入数据')
        return
      }
      obj.tags.push(obj.inputValue.trim())
      obj.inputValue = ''
    }
  },
  directives: {
    fff: {
      inserted(el) {
        el.focus()
      }
    }
  }
};
</script>
<style lang="less" scoped>
  .my-goods-list {
    .badge {
      margin-right: 5px;
    }
  }
  </style>